package com.echo.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.echo.entity.Schedule;
import com.echo.mapper.ScheduleDatesMapper;
import com.echo.mapper.ScheduleMapper;
import com.echo.WebSocket.WebSocketServer;
import com.echo.vo.ScheduleRemindVo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class ScheduleRemindTimeTask {
    private RedisTemplate redisTemplate;
    private ScheduleMapper scheduleMapper;
    private ScheduleDatesMapper scheduleDatesMapper;
    private static final ObjectMapper objectMapper = new ObjectMapper();
    //手动注入，通过ApplicationContext 获取 Bean
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    @Autowired
    public ScheduleRemindTimeTask(ApplicationContext applicationContext) {
        this.scheduleMapper = applicationContext.getBean(ScheduleMapper.class);
        this.scheduleDatesMapper = applicationContext.getBean(ScheduleDatesMapper.class);
    }
    //设置每1分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    public void RemindTime() throws IOException {
        // todo:这里的日程提醒暂时弄掉了：
        //这里的话就是每一分钟会执行这个方法，也就是要做什么处理这里可以做
        log.info("检测是否有提醒日程");
        /*String userId = (String) redisTemplate.opsForValue().get("userId");
        System.out.println(userId);
        LocalTime now = LocalTime.now();
        LocalDate dateNow = LocalDate.now();
        System.out.println(dateNow);
        System.out.println(userId==null);
        if (userId != null && !"null".equals(userId)) {
            QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .eq(Schedule::getUserId, userId)
                    .eq(Schedule::getStatus, false)
                    .eq(Schedule::getRemindTime, now)
                    .apply("EXISTS (SELECT 1 FROM schedule_dates WHERE schedule_id = schedule.schedule_id AND date = {0})", dateNow);


            List<Schedule> schedules = scheduleMapper.selectList(wrapper);

            if (!schedules.isEmpty()) {
                List<ScheduleRemindVo> scheduleRemindList = new ArrayList<>();
                for (Schedule schedule : schedules) {
                    ScheduleRemindVo scheduleRemindVo = new ScheduleRemindVo();
                    BeanUtils.copyProperties(schedule, scheduleRemindVo);
                    scheduleRemindList.add(scheduleRemindVo);
                }

                // 发送提醒
                String json = objectMapper.writeValueAsString(scheduleRemindList);
                WebSocketServer.sendInfo(json, userId);
            }
        }*/
    }
    /**
     * 处理支付超时订单
     */

}
